#!/usr/bin/env jython
from java.awt import Font, FlowLayout
from javax.swing import JPanel, JFrame, JLabel, BoxLayout, JButton, JTextArea, WindowConstants, JScrollPane, JFileChooser

import time
from CSVTools import CSVData
from Rebides import RebidesDB

from unicodedata import normalize
from Statistics import RebideStats

class Rebides2( JFrame ):
    csvList = []
    txtArea =  JTextArea(10,20)
    txtDBName = JTextArea( 1, 20 )
    txtTBName = JTextArea( 1, 20 )
    txtBDSelect =  JTextArea( 1, 20 )
    txtCsvName =  JTextArea( 1, 20 )
    txtHtmlName =  JTextArea( 1, 20 )
    
    def __init__( self ):
        self.setTitle( "Jython Application" )
        self.add( self.makeConvertPanel( self.makeTextArea( self.txtArea ), self.makeTextArea( self.txtDBName ), self.makeTextArea( self.txtTBName ) ) )
        self.add( JPanel() )
        self.add( self.makeStatisticsPanel( self.makeTextArea( self.txtBDSelect ), self.makeTextArea( self.txtCsvName ), self.makeTextArea( self.txtHtmlName ) ) )
        pass
    pass

    def evt_btnAdd( self, event ):
        path = self.chooseFile( True )
        if ( path != None ):
            self.txtArea.append( path + "\n" )
            self.csvList.append( path )
        pass
    pass

    def evt_btnClear( self, event ):
        self.txtArea.setText( "" )
        self.txtDBName.setText( "" )
        self.txtTBName.setText( "" )
        
        for i in xrange( 0, len(  self.csvList ) ):
            self.csvList.pop()
        pass
    pass

    def evt_btnConvert( self, event ):
        for filename in self.csvList:
            t0 = time.time()
            RebidesDB().addIntoDatabase( filename, self.txtDBName.getText(), self.txtTBName.getText() )
            pass
        print time.time() - t0
        pass
    pass

    def normalizeString( self, text, codif='utf-8' ):
        return normalize( 'NFKD', text.decode( codif ) ).encode( 'ASCII','ignore' )
    
    
    def evt_btnSearch( self, event ):
        path = self.chooseFile( True )
        self.txtBDSelect.setText( path )
        pass
    pass

    def evt_btnGenCsv( self, event ):
        t0 = time.time()
        RebideStats().CSVStatistics( self, self.txtBDSelect.getText(), self.txtCsvName.getText() )
        print time.time() - t0
        pass
    pass

    def evt_btnGenHtml( self, event ):
        t0 = time.time()
        RebideStats().HTMLStatistics(self , self.txtBDSelect.getText(), self.txtHtmlName.getText() )
        print time.time() - t0
        pass
    pass

    def chooseFile( self, toOpen ):
        chooser = JFileChooser();
      
        #chooser.setFileFilter( FileNameExtensionFilter( "Text files", "txt" ) );
        
        if ( toOpen == True ):
            returnVal = chooser.showOpenDialog(self)
            pass
        else:
            returnVal = chooser.showSaveDialog(self)
            pass
        
        if (returnVal == JFileChooser.APPROVE_OPTION):
            return chooser.getSelectedFile().getAbsolutePath();
        else:
            return None
        pass
    pass

    def makeConvertPanel( self, textArea, txtDB, txtTB ):
        panel = JPanel() 
        panel.setLayout( BoxLayout( panel, BoxLayout.Y_AXIS) )   

        panel.add( JLabel( "Select a .csv files to be converted in a sqlite database" ) )
        panel.add( JPanel() )

        panel.add( textArea )
        panel.add( JPanel() )

        subPanel = JPanel() 
        subPanel.setLayout( FlowLayout() )
        subPanel.add( JButton( "Add", actionPerformed = self.evt_btnAdd ) )
        subPanel.add( JPanel() )
        subPanel.add( JButton( "Clear", actionPerformed = self.evt_btnClear ) ) 

        panel.add( subPanel )
        panel.add( JPanel() )

        hPanel = JPanel()
        hPanel.setLayout( BoxLayout( hPanel, BoxLayout.X_AXIS ) )

        vPanel = JPanel() 
        vPanel.setLayout( BoxLayout( vPanel, BoxLayout.Y_AXIS ) )

        subPanel = JPanel() 
        subPanel.setLayout( BoxLayout( subPanel, BoxLayout.X_AXIS ) )
        subPanel.add( JLabel( "Database Name: " ) )
        subPanel.add( txtDB )

        vPanel.add( subPanel )

        subPanel = JPanel() 
        subPanel.setLayout( BoxLayout( subPanel, BoxLayout.X_AXIS ) )
        subPanel.add( JLabel( "Table Name: " ) )
        subPanel.add( txtTB )
        vPanel.add( subPanel )

        hPanel.add( vPanel )
        hPanel.add( JButton( "Convert", actionPerformed = self.evt_btnConvert ) )

        panel.add( hPanel )
        return panel
        pass
    pass

    def makeStatisticsPanel( self, txtCsv, txtCsvName, txtHtmlName):
        panel = JPanel() 
        panel.setLayout( BoxLayout( panel, BoxLayout.Y_AXIS) )  
        panel.add( JLabel( "Select a Rebides database to generate statistics" ) )
        panel.add( JPanel() )
        
        subPanel = JPanel() 
        subPanel.setLayout( BoxLayout( subPanel, BoxLayout.X_AXIS ) )
        subPanel.add( JButton( "Search", actionPerformed = self.evt_btnSearch ) )
        subPanel.add( txtCsv )
        panel.add( subPanel )
        
        subPanel = JPanel() 
        subPanel.setLayout( BoxLayout( subPanel, BoxLayout.X_AXIS ) )
        
        
        csvPanel = JPanel() 
        csvPanel.setLayout( BoxLayout( csvPanel, BoxLayout.Y_AXIS ) )  
        
        csvSubPanel = JPanel() 
        csvSubPanel.setLayout( BoxLayout( csvSubPanel, BoxLayout.X_AXIS ) )      
        csvSubPanel.add( JLabel( "CSV Name: " ) )
        csvSubPanel.add( txtCsvName )
        
        csvPanel.add( csvSubPanel )
        csvPanel.add( JPanel() )
        csvPanel.add( JButton( "Generate .csv files", actionPerformed = self.evt_btnGenCsv ) )
        
        
        htmlPanel = JPanel() 
        htmlPanel.setLayout( BoxLayout( htmlPanel, BoxLayout.Y_AXIS ) )  
        
        htmlSubPanel = JPanel() 
        htmlSubPanel.setLayout( BoxLayout( htmlSubPanel, BoxLayout.X_AXIS ) )      
        htmlSubPanel.add( JLabel( "HTML Name: ") )
        htmlSubPanel.add( txtHtmlName )
        
        htmlPanel.add( htmlSubPanel )
        htmlPanel.add( JPanel() )
        htmlPanel.add( JButton( "Generate HTML file", actionPerformed = self.evt_btnGenHtml ) )
        
        
        subPanel.add( csvPanel )
        subPanel.add( JPanel() )
        subPanel.add( htmlPanel )
        
        panel.add( JPanel() )
        panel.add( subPanel )
        return panel
        pass
    pass
           
    def showFrame( self ):
        frame.setLayout( FlowLayout() )
        frame.setResizable( False )
        frame.pack()
        frame.toFront()
        frame.setVisible( True )
        frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE )
        pass
    pass

    def makeTextArea( self, outputTextArea ):
        outputTextArea.setFont( Font( "Verdana", Font.PLAIN, 14 ) )
        outputTextArea.setEditable( True )
        vScroll = JScrollPane( outputTextArea );
        vScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED )
        vScroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER )
        return vScroll
    pass
pass


if __name__ == "__main__":
    frame = Rebides2()
    frame.showFrame()
    pass
pass